package at.exam;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author Do Hong Thang
 * 
 */
public class DBAdapter {

	// Province table
	public static final String PROVINCE_TABLE = "Province";
	public static final String PROVINCE_ID = "id";
	public static final String PROVINCE_NAME = "name";

	// Lottery Result
	public static final String LOTTERY_TABLE = "Lottery";
	public static final String PROVINCE_CODE = "code";

	public static final String OPEN_DATE = "OpenDate";
	public static final String SPECIAL_0 = "Special_0";
	public static final String SPECIAL_1 = "Special_1";
	public static final String FIRST = "first";
	public static final String SECOND = "second";
	public static final String THIRD = "third";
	public static final String FOURTH = "fourth";
	public static final String FIFTH = "fifth";
	public static final String SIXTH = "sixth";
	public static final String SEVENTH = "seventh";
	public static final String EIGHTH = "eighth";

	// Database name
	private static final String DATABASE_NAME = "KQXS_Database";

	// Database Version
	private static final int DATABASE_VERSION = 1;

	private static final String TAG = "DBAdapter";

	private static final String PROVINCE_TABLE_CREATE = "CREATE TABLE "
			+ PROVINCE_TABLE + " (" + PROVINCE_ID + " TEXT, " + PROVINCE_NAME
			+ " TEXT)";

	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				db.execSQL(PROVINCE_TABLE_CREATE);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS Data");
			onCreate(db);
		}
	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	/**
	 * 
	 */
	public void close() {
		DBHelper.close();
	}

	/**
	 * Insert a province into the Province table
	 * 
	 * @param id
	 * @return Name
	 */
	public long insertProvince(String id, String name) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(PROVINCE_ID, id);
		initialValues.put(PROVINCE_NAME, name);
		return db.insert(PROVINCE_TABLE, null, initialValues);
	}
	
	/**
	 * 
	 * @param special_0
	 * @param special_1
	 * @param first
	 * @param second
	 * @param third
	 * @param fourth
	 * @param fifth
	 * @param sixth
	 * @param seventh
	 * @param eighth
	 * @return
	 */
	public long insertLotteryResult(int special_0, int special_1, int first,
			int second, int third, int fourth, int fifth, int sixth,
			int seventh, int eighth) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(DBAdapter.SPECIAL_0, special_0);
		initialValues.put(DBAdapter.SPECIAL_1, special_1);
		initialValues.put(DBAdapter.FIFTH, first);
		initialValues.put(DBAdapter.SECOND, second);
		initialValues.put(DBAdapter.THIRD, third);
		initialValues.put(DBAdapter.FOURTH, fourth);
		initialValues.put(DBAdapter.FIFTH, fifth);
		initialValues.put(DBAdapter.SIXTH, sixth);
		initialValues.put(DBAdapter.SEVENTH, seventh);
		initialValues.put(DBAdapter.EIGHTH, eighth);
		return db.insert(LOTTERY_TABLE, null, initialValues);
	}

	// ---retrieves all the Prize---
	public Cursor getAllPrize() {
		return db.query(LOTTERY_TABLE, new String[] { SPECIAL_0, SPECIAL_1,
				FIRST, SECOND, THIRD, FOURTH, FIFTH, SIXTH, SEVENTH, EIGHTH },
				null, null, null, null, null, null);
	}
}